﻿#include "chartview.h"
#include "ui_chartview.h"



ChartView::ChartView(QWidget* parent) :
	QWidget(parent),
	ui(new Ui::ChartView)
{
	ui->setupUi(this);
	QStringList labels;
	labels << TM;
	m_row1 = 0;
	m_row2 = 0;
	m_rowMax2 = 100;
    m_nView = 0;
	m_bState = false;
	m_bRun = false;
	ui->widget->setOrientation(ChartPane::VERTICAL_BAR);
	ui->widget->setLabels(labels);
	pThread = new ChartThread();
	m_qTimer = new QTimer(this);
	m_qTimer->setInterval(1000);
	connect(pThread->m_worker, &ChartWorker::sigChartInfo, this, &ChartView::sloInfo);
	connect(m_qTimer, SIGNAL(timeout()), this, SLOT(timerSlot()));
}

ChartView::~ChartView()
{
	if (m_bRun)
	{
		pThread->stop();
		QThread::msleep(2000);
	}
	delete ui;
}



void ChartView::setTitle(QString name)
{
	m_sTitle = name;
}

void ChartView::clear()
{
	ui->widget->m_nIndex = 0;
	ui->widget->m_nRow = 0;
	ui->widget->m_vIndexs.clear();
	ui->widget->m_vIndexs.push_back(0);
}

void ChartView::DataInit(QString file, Orientation type)
{
	clear();
	m_sFile = file;
	setTitle(QFileInfo(file).fileName());
	ui->widget->setTitle(m_sTitle);
    ui->widget->m_dealData.m_nView = m_nView;

	switch (type) {
	case BARCHART_VIEW:
		ui->widget->setFile(file, 1);
		break;
	case LINECHART_VIEW:
		ui->widget->setFile(file, 0);
		break;
	case SPLINE_VIEW:
		ui->widget->showUI();
		if (!m_bRun)
		{
			bool bState = ui->widget->getTabState(file);
			if (!bState)
			{
				m_strName = file;
				m_bRun = true;
				m_row1 = m_row2 = 0;
				m_rowMax2 = 100;
				ui->widget->setPorssBarValue(m_row1, m_row2, m_rowMax2);
				m_qTimer->start();
				pThread->start(file);
			}
			else {
				ui->widget->setFile(m_sFile, 2);
			}
		}
		else
		{
			QString strInfo = QString(tr("is end %1")).arg(m_strName);
			if (QMessageBox::Yes == QMessageBox::information(this, tr("Title"), strInfo, QMessageBox::Yes | QMessageBox::No))
			{
				m_bRun = false;
				pThread->stop();
			}
			else
			{
			}
		}
		break;
	}
	ui->widget->m_vIndexs.push_back(ui->widget->m_nRow);
}

void ChartView::timerSlot()
{
	pThread->m_worker->getProssBarValue(m_row1, m_row2, m_rowMax2);
	ui->widget->setPorssBarValue(m_row1, m_row2, m_rowMax2);
}

void ChartView::sloInfo()
{
	m_qTimer->stop();

	if (m_bRun)
	{
		ui->widget->setFile(m_strName, 2);
		m_bRun = false;
	}
	else {
		DataInit(m_sFile, SPLINE_VIEW);
	}

}


